package com.itheima03.day16test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 刘全
 * @date 2021/8/10
 */
public class ServerTest5 {
    public static void main(String[] args) throws IOException {
        /**
         * 5. 使用TCP完成数据的发送和接收
         *     发送端发送，接收端接收
         *     接收端回复，发送端接收
         */
        ServerSocket ss = new ServerSocket(10086);

        Socket accept = ss.accept();
        InputStream is = accept.getInputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = is.read(bytes)) != -1) {
            System.out.print(new String(bytes,0,len));
            System.out.println();
        }

        OutputStream os = accept.getOutputStream();
        Scanner sc = new Scanner(System.in);
        String str;
        while (true) {
            str = sc.nextLine();
            if ("886".equals(str)) {
                break;
            }
            os.write(str.getBytes());
        }
        os.flush();
        os.close();
        is.close();
        accept.close();
        ss.close();
    }
}
